package com.gyms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.okhttp.Cons;
import com.classic.okhttp.beans.HVProductInfoBean;
import com.gyms.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import constant.PicSplit;
import glide.ImageLoaderUtils;
import utils.DecimalUtils;
import utils.StringUtils;

import static com.gyms.R.id.tv_course_buy;

/**
 * ltx on 2016/11/16 16:46
 * 在售课程adapter
 */
public class OlineCourseAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ViewHolder mViewHolder;
    // 场馆优惠活动
    private ArrayList<HVProductInfoBean> products;

    public OlineCourseAdapter(Context mContext) {
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return null==products?0:products.size();
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.online_course_item, parent, false);
            mViewHolder=new ViewHolder(convertView);
          /*  mViewHolder.mTvCourseBuy.setOnClickListener(this);*/
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder= (ViewHolder) convertView.getTag();
        }
        HVProductInfoBean mHVProductBean=products.get(position);//在售课程的bean
        doFullView(mHVProductBean);
        return convertView;
    }

    /**
     * 填充数据
     * @param mHVProductBean 在售课程bean
     */

    private void doFullView(HVProductInfoBean mHVProductBean) {
        if(!StringUtils.isNull(mHVProductBean.getDiscount())){//如果优惠图标为null就隐藏imageview
            String discountUrl=mHVProductBean.getDiscount().get(0).getDiscountIcon();
            mViewHolder.mTvHuiText.setText(mHVProductBean.getDiscount().get(0).getDiscountContent());
            if(!StringUtils.isNull(discountUrl)){//优惠的图片为null隐藏
                mViewHolder.mTvDiscount.setVisibility(View.VISIBLE);
                mViewHolder.mTvHuiText.setVisibility(View.VISIBLE);

                ImageLoaderUtils.displayBigPhoto(mContext, mViewHolder.mTvDiscount, discountUrl);
            }
        }else{
            mViewHolder.mTvDiscount.setVisibility(View.GONE);
            mViewHolder.mTvHuiText.setVisibility(View.GONE);
        }

        //是否可退款
        mViewHolder.mIvRefund.setVisibility(mHVProductBean.refundable?View.VISIBLE:View.GONE);
        mViewHolder.mTvRefundText.setVisibility(mHVProductBean.refundable?View.VISIBLE:View.GONE);
        if(mHVProductBean.refundable){
            ImageLoaderUtils.display(mContext,mViewHolder.mIvRefund,  PicSplit.urlAdd(Cons.S_PRODUCT_REFUND_ICON,PicSplit.TYPE_TAB));
        }
        ImageLoaderUtils.display(mContext,mViewHolder.mIvCorse,mHVProductBean.getProductUrl());
        mViewHolder.mTvCourseName.setText(mHVProductBean.getProductName());
        double minPrice=mHVProductBean.getMinPrice();
        double maxPrice=mHVProductBean.getMaxPrice();
        if(minPrice==maxPrice){//最低价等于最高价显示一个
            mViewHolder.mTvCoursePrice.setText( DecimalUtils.setPriceStyle(mContext,minPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));

        }else{
//        mViewHolder.mTvCoursePrice.setText("￥"+ DecimalUtils.doubleToIntOrD(minPrice)+"-"+DecimalUtils.doubleToIntOrD(maxPrice));
            mViewHolder.mTvCoursePrice.setText(DecimalUtils.setPriceStyle(mContext,minPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true).append(" - ").append( DecimalUtils.setPriceStyle(mContext,maxPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,false)));
        }

    }

    public void setProducts(ArrayList<HVProductInfoBean> products) {
        this.products = products;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case tv_course_buy:

                break;
        }

    }

    static class ViewHolder {
        @BindView(R.id.tv_course_name)
        TextView mTvCourseName;
        @BindView(R.id.tv_discount)
        ImageView mTvDiscount;
        @BindView(R.id.tv_hui_text)
        TextView mTvHuiText;
        @BindView(R.id.tv_course_buy)
        TextView mTvCourseBuy;
        @BindView(R.id.tv_course_price)
        TextView mTvCoursePrice;
        @BindView(R.id.iv_corse)
        ImageView mIvCorse;

        @BindView(R.id.tv_refund_text)
        TextView mTvRefundText;

        @BindView(R.id.iv_refund)
        ImageView mIvRefund;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
